package mpcs.utils
{
	import com.google.maps.LatLng;
	import com.google.maps.Map3D;
	import com.google.maps.MapEvent;
	import com.google.maps.MapType;
	import com.google.maps.View;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.controls.MapTypeControlOptions;
	import com.google.maps.controls.NavigationControl;
	import com.google.maps.controls.OverviewMapControl;
	import com.google.maps.controls.OverviewMapControlOptions;
	
	import mpcs.utils.setting.Settings;

	/**
	 *  Google Map 对象
	 * @author zhangzuoqiang
	 */	
	public class GoogleMap {
		
		private var map:Map3D;
		
		public var settings:Settings;
		
		/**map属性key**/
		public var key:String;
		/**map属性src**/
		public var src:String;		
		/**地图缩放级别**/
		public var ZOOM:int = 15;
		
		private var startX:Number = 0;
		private var startY:Number = 0;
		// 初始化公共变量 LATLNG
		private var LATLNG:LatLng = null;
		
		/**
		 *  创建完GoogleMap 后不要忘记初始化其参数initGMap
		 */		
		public function GoogleMap() {
			map = new Map3D();
			map.addEventListener(MapEvent.MAP_PREINITIALIZE, mapPreinitialize);
			map.addEventListener(MapEvent.MAP_READY, mapreadyHandler);
			loadpreValue();
		}
		
		/**
		 *  初始化Map的基本属性，必须的！！！
		 */		
		public function initGMap():void {
			
			trace("GoogleMap ...  initGMap ... ");
			
			map.sensor = "true";
			map.key = key;
			map.url = src;
		}
		
		/**
		 * 加载地图相关配置文件
		 */
		private function loadpreValue():void{
			
			trace("GoogleMap ... loadpreValue ... ");
			
			if( settings == null ){
				settings = new Settings();
				initSettingValues();
			}
		}
		
		/**
		 *  初始化地图的基本属性
		 */		
		private function initSettingValues():void {
			key = settings.key;
			src = settings.url;
			startX = settings.x;
			startY = settings.y;
		}
		
		protected function mapPreinitialize(event:MapEvent):void {
			trace("GoogleMap ...  mapPreinitialize ... ");
			map.enabled = false;
		}
		
		protected function mapreadyHandler(event:MapEvent):void {
			
			map.removeEventListener(MapEvent.MAP_PREINITIALIZE, mapPreinitialize);
			map.removeEventListener(MapEvent.MAP_READY, mapreadyHandler);
			
			trace("GoogleMap ...  mapreadyHandler ... ");
			
			map.overlayRaising = true;
			//显示一个地图的缩略图 
			map.addControl( new OverviewMapControl( new OverviewMapControlOptions() ) );
			//在地图上显示地图的4种类型，可以随意切换 
			map.addControl( new MapTypeControl( new MapTypeControlOptions() ) );
			//增加地图的3D控制
			map.addControl(new NavigationControl());
			map.enableScrollWheelZoom(); 
			map.enableContinuousZoom(); 
			//启用中心十字准线
			map.enableCrosshairs();
			map.viewMode = View.VIEWMODE_PERSPECTIVE;
			this.map.setCenter(new LatLng(startX,startY)/*经纬度*/, ZOOM,  MapType.SATELLITE_MAP_TYPE);
			
			map.enabled = true;
			
			LATLNG = new LatLng(startX, startY);
//			latlng2Addr();
		}
		
	}
}